home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Communication / NewsBase / Source / common.subproj / IOrderedListD.m < prev    next >
Text File  |  1993-01-12  |  3KB  |  123 lines

  1. #import "IOrderedListD.h"
  2. #import <objc/hashtable.h>
  3. #import <string.h>
  4.  
  5. @implementation IOrderedListD
  6.  
  7. - initWithKey:(const char *)aKey
  8. {
  9.     [super init];
  10.     key = NXCopyStringBufferFromZone(aKey, [self zone]);
  11.     return(self);
  12. }
  13.  
  14. - (const char *)key
  15. {
  16.     return(key);
  17. }
  18.  
  19. - free
  20. {
  21.     NXZoneFree([self zone], key);
  22.     [self freeObjects];
  23.     return([super free]);
  24. }
  25.  
  26. - (BOOL)insertKeyedObject:(IKeyedObject *)theObject
  27. {
  28.     unsigned int index;
  29.     BOOL flag;
  30.  
  31.     index = [self indexForKey:[theObject key] exists:&flag];
  32.     if (flag == NO) {
  33.         [self insertObject:theObject at:index];
  34.         return(YES);
  35.     } else {
  36.         return(NO);   
  37.     }
  38. }
  39.  
  40. - objectWithKey:(const char *)theKey
  41. {
  42.     unsigned int index;
  43.     BOOL flag;
  44.  
  45.     index = [self indexForKey:theKey exists:&flag];
  46.     if (flag == YES) {
  47.         return([self objectAt:index]);
  48.     } else {
  49.         return(nil);   
  50.     }
  51. }
  52.  
  53. - dataForKey:(const char *)theKey
  54. {
  55.     return([self objectWithKey:theKey]);
  56. }
  57.  
  58. - (BOOL)removeObjectWithKey:(const char *)theKey
  59. {
  60.     unsigned int index;
  61.     BOOL flag;
  62.  
  63.     index = [self indexForKey:theKey exists:&flag];
  64.     if (flag == YES) {
  65.         [self removeObjectAt:index];
  66.         return(YES);
  67.     } else {
  68.         return(NO);   
  69.     }
  70. }
  71.  
  72. - (BOOL)removeDataForKey:(const char *)theKey
  73. {
  74.     return([self removeObjectWithKey:theKey]);
  75. }
  76.  
  77. - (unsigned int)indexForKey:(const char *)theKey exists:(BOOL *)flag
  78. {
  79.     unsigned int lowerBound, midPoint, upperBound;
  80.     int strcmpResult;
  81.  
  82.     if (numElements == 0) {
  83.         *flag = NO;
  84.         return(0);
  85.     }
  86.     lowerBound = 0;
  87.     upperBound = numElements - 1;
  88.     strcmpResult = strcmp(theKey, [[self objectAt:lowerBound] key]);
  89.     if (strcmpResult == 0) {
  90.         *flag = YES;
  91.         return(lowerBound);
  92.     }
  93.     if (strcmpResult < 0) {
  94.         *flag = NO;
  95.         return(lowerBound);
  96.     }
  97.     strcmpResult = strcmp(theKey, [[self objectAt:upperBound] key]);
  98.     if (strcmpResult == 0) {
  99.         *flag = YES;
  100.         return(upperBound);
  101.     }
  102.     if (strcmpResult > 0) {
  103.         *flag = NO;
  104.         return(upperBound + 1);
  105.     }
  106.     while(upperBound - lowerBound > 1) {
  107.         midPoint = (lowerBound + upperBound) / 2;
  108.         strcmpResult = strcmp(theKey, [[self objectAt:midPoint] key]);
  109.         if (strcmpResult == 0) {
  110.             *flag = YES;
  111.             return(midPoint);
  112.         } else if (strcmpResult < 0) {
  113.             upperBound = midPoint;
  114.         } else {
  115.             lowerBound = midPoint;
  116.         }
  117.     }
  118.     *flag = NO;
  119.     return(upperBound);
  120. }
  121.  
  122. @end
  123.